﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Windows.Forms;

namespace Tornado
{

    class WinPower
    {
        [DllImport("user32.dll")]
        static extern int SendMessage(int hWnd, uint Msg, int wParam, int lParam);
        [DllImport("user32.dll", SetLastError = true)]
        static extern int FindWindow(string lpClassName, string lpWindowName);
        [DllImport("user32.dll")]
        public static extern int ExitWindowsEx(int operationFlag, int operationReason);
        [DllImport("user32.dll", SetLastError = true)]
        static extern bool LockWorkStation();

        const int SC_SCREENSAVE = 0xF140;
        const int SC_MONITORPOWER = 0xF170;
        const int WM_SYSCOMMAND = 0x0112;

        const int MONITOR_ON = -1;
        const int MONITOR_OFF = 2;
        const int MONITOR_STANDBY = 1;

        static int ValidHWND = FindWindow(null, null);
        static public void MonitorOff()
        {
            SendMessage(ValidHWND, WM_SYSCOMMAND, SC_MONITORPOWER, MONITOR_OFF);
        }

        static public void MonitorOn()
        {
            SendMessage(ValidHWND, WM_SYSCOMMAND, SC_MONITORPOWER, MONITOR_ON);
        }

        static public void Standby()
        {
            SendMessage(ValidHWND, WM_SYSCOMMAND, SC_MONITORPOWER, MONITOR_STANDBY);
        }

        static public void ScreenSave()
        {
            SendMessage(ValidHWND, WM_SYSCOMMAND, SC_SCREENSAVE, 0);
        }

        static public void Lock()
        {
            LockWorkStation();
        }

        static public void LogOff()
        {
            ExitWindowsEx(4, 0);
        }

        static public void Shutdown()
        {
            ProcessStartInfo inf = new ProcessStartInfo("shutdown.exe","-s -t 00 -f");
            inf.CreateNoWindow = true;
            Process.Start(inf);
        }

        static public void Restart()
        {
            ProcessStartInfo inf = new ProcessStartInfo("shutdown.exe", "-r -t 00 -f");
            inf.CreateNoWindow = true;
            Process.Start(inf);
        }

        static public void Hibernate()
        {
            Application.SetSuspendState(PowerState.Hibernate, false, false);
        }

        static public void ChangeScreen(string type)
        {
            ProcessStartInfo inf = new ProcessStartInfo("DisplaySwitch.exe", "/" + type);
            inf.CreateNoWindow = false;
            Process.Start(inf);
        }
    }
}
